package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author LiangTianQi
 * @describe /ordersetting/upload.do
 * @date 2020/11/11 22:03
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        try {
            List<String[]> sheet = POIUtils.readExcel(multipartFile);
            if (sheet != null && sheet.size() > 0) {
                for (String[] row : sheet) {
                    //循环数组中的数据，将数据封装到，ordersetting的bean中
                    OrderSetting orderSetting = new OrderSetting();
                    for (int i = 0; i < row.length; i++) {
                        //进行判断
                        if (row[i].contains("/")){
                            Date data = new SimpleDateFormat("yyyy/MM/dd").parse(row[i]);
                            orderSetting.setOrderDate(data);
                        }else{
                            int number = Integer.parseInt(row[i]);
                            orderSetting.setNumber(number);
                        }
                    }
    //                System.out.println(orderSetting);
                    orderSettingService.add(orderSetting);
                }
            }
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"上传失败");
        }

    }

    @GetMapping("/getOrdersettingByMonth")
    //前端传来当前的年和月
    public Result getOrdersettingByMonth(String date) {
        try {
            List<OrderSetting> list=orderSettingService.getOrdersettingByMonth(date);

            //过滤不需要的字段
            /*SimplePropertyPreFilter filter = new SimplePropertyPreFilter(OrderSetting.class,"id","orderDate");
            String string = JSONObject.toJSONString(list, filter);*/
            PropertyFilter profilter = new PropertyFilter(){
                @Override
                public boolean apply(Object object, String name, Object value) {
                    if(name.equalsIgnoreCase("id")||name.equalsIgnoreCase("orderDate")){
                        //false表示last字段将被排除在外
                        return false;
                    }
                    return true;
                }
            };
            String string = JSON.toJSONString(list, profilter);

            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, string);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.add(orderSetting);
            return new Result(true,"更改预约设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"更改预约设置失败");
        }

    }

    @GetMapping("/setNumber")
    public Result setNumber(Integer number) {
        //将该数据保存到redis中
        try {
            orderSettingService.setNumber(number);
            return new Result(true,"更改默认预约人数设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更改默认预约人数设置失败");
        }
    }
}
